/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.service
 * 创建时间：2020年04月08日 15时52分
 **/
package com.zhongwang.mms.module.purchase.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.AllocationOrder;
import com.zhongwang.mms.gen.entity.PInventory;
import com.zhongwang.mms.gen.entity.POrder;
import com.zhongwang.mms.gen.entity.POrderDetail;
import com.zhongwang.mms.module.half.dao.PAllocationRecordDao;
import com.zhongwang.mms.module.purchase.dao.*;

import com.zhongwang.mms.module.purchase.model.*;

import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName： ReverseService
 * @Description：
 * @Author： 徐一贺
 * @Date： 2020/4/8 15:52
 * @Version： V1.0
 **/
@Log4j2
@Service
public class PurReverseService {
    @Autowired
    private PurReverseDao purReverseDao;
    @Autowired
    private PurPOrderDao purPOrderDao;
    @Autowired
    private PurPOrderDetailDao purPOrderDetailDao;
    @Autowired
    private PurPInventoryDao purPInventoryDao;
    @Autowired
    private PAllocationRecordDao pAllocationRecordDao;
    @Autowired
    private LogService logService;
    @Autowired
    private PurChangeDao purChangeDao;

    /**
     * @FunctionName: findReverseList
     * @Description: 冲单页面列表查询
     * @Author: 徐一贺
     * @CreateDate: 2020/4/8 15:29
     * @Phone: 18241927380
     * @Param: [reverseQueryModel]
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version:
     */
    public List<ReverseModel> findReverseList(ReverseModel param) {
        if (param.getEndDate() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(param.getEndDate());
            cal.add(Calendar.DAY_OF_YEAR, 1);
            param.setEndDate(cal.getTime());
        }
        return purReverseDao.findReverseList(param);
    }

    /**
     * @FunctionName: reverseOrder
     * @Description: 执行冲单，检测所冲单是否是入库单，如果是入库单则验证库存数量
     * 是否足够执行对入库单冲单的操作。
     * 冲单：冲掉原数据中的明细，生成一条明细单数量为负，并和被冲记录做关联，
     * 被冲记录标识为被红冲数据，冲单数据标识为红冲数据。
     * 串料调拨单冲单时，只能冲整单，并且冲单后要更改其对应的串料申请单的执行状态。
     * 库存被拆数据不可红冲，串料、划库存 划项目
     * 如果因调整造成出入库单价、单位、物料描述发生变化，则冲回来的数据重新生成
     * 一条新的库存，新的行号与冲单明细主键关联。
     * @Author: 徐一贺
     * @CreateDate: 2020/4/9 8:43
     * @Phone: 18241927380
     * @Param: param
     * @Return: boolean
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult reverseOrder(ReverseParam param) {
        //批次处理
        if (param.getType().equals("batch")) {
            //判断是入库还是出库
            List<POrderModel> pOrderList = purPOrderDao.selectPOrderModelByOdaIds(param.getOdaIds());
            String outIds = "";
            String inIds = "";
            for (POrderModel pOrder : pOrderList) {
                if (pOrder.getOdrType().equals(1)) {
                    inIds += pOrder.getOdaId() + ",";
                } else if (pOrder.getOdrType().equals(2)) {
                    outIds += pOrder.getOdaId() + ",";
                }
            }
            //获取单据明细与库存信息对象
            List<POrderDetailAndPInventoryModel> pOrderDetailAndPInventoryModelList = new ArrayList<>();

            if (!inIds.equals("")) {
                inIds = inIds.substring(0, inIds.length() - 1);
                pOrderDetailAndPInventoryModelList.addAll(purPOrderDetailDao.findPOrderDetailAndPInventoryModelListByOdrId(inIds));
            }
            if (!outIds.equals("")) {
                outIds = outIds.substring(0, outIds.length() - 1);
                pOrderDetailAndPInventoryModelList.addAll(purPOrderDetailDao.findPOrderDetailAndPInventoryModelListByOdrIdOut(outIds));
            }
            for (int j = 0; j < pOrderDetailAndPInventoryModelList.size(); j++) {
                if (pOrderDetailAndPInventoryModelList.get(j).getOdrType() == 1 &&
                        pOrderDetailAndPInventoryModelList.get(j).getOdrRedFlag() == 0 &&
                        pOrderDetailAndPInventoryModelList.get(j).getOdrChangeFlag() == 0 &&
                        pOrderDetailAndPInventoryModelList.get(j).getOdrReqFlag() == 0 &&
                        pOrderDetailAndPInventoryModelList.get(j).getOdaNum() > pOrderDetailAndPInventoryModelList.get(j).getItyNum()) {
                    return JsonResult.fail("批量红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModelList.get(j).getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModelList.get(j).getOdaMatDesc() + "的库存数量不足！");
                }
                //判断是否包含两条库存数据
                Integer pInventoryCount = 0;
                if (!inIds.equals("")) {
                    pInventoryCount = purPOrderDetailDao.getPInventoryCountByRowNo(pOrderDetailAndPInventoryModelList.get(j).getOdaId());
                } else if (!outIds.equals("")) {
                    pInventoryCount = purPOrderDetailDao.getPInventoryCountByOutItyId(pOrderDetailAndPInventoryModelList.get(j).getOdaId());
                }
                if (pInventoryCount > 1 && pOrderDetailAndPInventoryModelList.get(j).getOdrType().equals(1)) {
                    return JsonResult.fail("批量红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModelList.get(j).getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModelList.get(j).getOdaMatDesc() + "的对应库存被串料或调拨成多条库存！");
                }
                //判断调拨单不能单冲
                if (pOrderDetailAndPInventoryModelList.get(j).getOdrReqFlag() == 1) {
                    return JsonResult.fail("批量红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModelList.get(j).getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModelList.get(j).getOdaMatDesc() + "的身份是调拨单不可只红冲明细！");
                }
                //判断串料出库单不能单冲
                if (pOrderDetailAndPInventoryModelList.get(j).getOdrType().equals(2)) {
                    POrderDetail pOrderDetail = purPOrderDetailDao.findOrderDetailByOdaId(pOrderDetailAndPInventoryModelList.get(j).getOdaId());
                    PInventory pInventory = purPInventoryDao.getPInventoryByPK(pOrderDetail.getOdaOutItyId());
                    if (pInventory.getItyChangeFlag() != null && (pInventory.getItyChangeFlag().equals(1) || pInventory.getItyChangeFlag().equals(3))) {
                        return JsonResult.fail("批量红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModelList.get(j).getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModelList.get(j).getOdaMatDesc() + "的身份是串出的库存不可批量红冲！");
                    }
                }
                //判断红冲单不能冲
                if (pOrderDetailAndPInventoryModelList.get(j).getOdrRedFlag() == 1) {
                    return JsonResult.fail("红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModelList.get(j).getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModelList.get(j).getOdaMatDesc() + "的身份是红冲单不可红冲！");
                }
            }
            //执行红冲
            for (int j = 0; j < pOrderDetailAndPInventoryModelList.size(); j++) {
                POrderDetail pOrderDetail = purPOrderDetailDao.findOrderDetailByOdaId(pOrderDetailAndPInventoryModelList.get(j).getOdaId());
                pOrderDetail.setOdaRedFlag(1);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                Integer odaId = pOrderDetail.getOdaId();
                //标记被红冲数据
                purPOrderDetailDao.updateByPrimaryKeySelective(pOrderDetail);
                Double redNum = 0 - pOrderDetail.getOdaNum();
                pOrderDetail.setOdaNum(redNum);
                pOrderDetail.setOdaRushId(pOrderDetail.getOdaId());
                pOrderDetail.setOdaRedFlag(2);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                pOrderDetail.setOdaBrand(pOrderDetail.getOdaBrand());
                //标记红冲数据
                purPOrderDetailDao.insert(pOrderDetail);
                //根据单据类型对库存操作
                PInventory pInventory = null;
                if (pOrderDetail.getOdaType().equals(1)) {
                    pInventory = purPInventoryDao.findInventoryByRowNumber(odaId);
                } else if (pOrderDetail.getOdaType().equals(2)) {
                    pInventory = purPInventoryDao.findInventoryByodaOutItyId(odaId);
                }

                String orderTypeText = "";
                if (pOrderDetail.getOdaType() == 1) {
                    orderTypeText = "入库单";
                    //如果是入库则减少库存
                    pInventory.setItyNum(pInventory.getItyNum() - Math.abs(redNum));
                    pInventory.setItyAvaNum(pInventory.getItyNum());
                } else if (pOrderDetail.getOdaType() == 2) {
                    orderTypeText = "出库单";
                    //如果是出库则增加库存
                    pInventory.setItyNum(pInventory.getItyNum() + Math.abs(redNum));
                    pInventory.setItyAvaNum(pInventory.getItyNum());
                }
                purPInventoryDao.updateNumByPrimaryKey(pInventory);
                logService.saveLog("批量红冲", "被红冲单号：" + pOrderDetailAndPInventoryModelList.get(j).getOdrCode() + ";被红冲物料名称：" + pOrderDetail.getOdaMatDesc() + ";单据类型：" + orderTypeText);
            }
        }
        //单条处理
        else if (param.getType().equals("single")) {
            POrderDetailAndPInventoryModel pOrderDetailAndPInventoryModel = purPOrderDetailDao.findPOrderDetailAndPInventoryModelListByOdaId(param.getOdaIds());
            if(pOrderDetailAndPInventoryModel.getOdrRedFlag() == 1){
                return JsonResult.fail("红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModel.getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModel.getOdaMatDesc() + "的身份是红冲单不可红冲！");
            }
            //判断当前单据明细关联的库存是否因库存划项目，项目化库存和串料导致生成多条数据
            Integer pInventoryCount = purPOrderDetailDao.getPInventoryCountByRowNo(pOrderDetailAndPInventoryModel.getOdaId());
            if (pInventoryCount > 1) {
                return JsonResult.fail("红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModel.getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModel.getOdaMatDesc() + "的对应库存被串料、库存划项目或项目划库存成多条库存！");
            }
            //判断是否是调拨出库单，验证成品库是否执行冲单，如果发现冲单，则执行冲整个调拨单
            if (pOrderDetailAndPInventoryModel.getOdrReqFlag() == 1 && pOrderDetailAndPInventoryModel.getOdaRedFlag() == 0) {
                //获取调拨单信息
                AllocationOrder allocationOrder = purPOrderDetailDao.getAllocationOrderByOdrCode(pOrderDetailAndPInventoryModel.getOdrRepId());
                List<POrderDetail> pOrderDetailList = purPOrderDetailDao.findOrderDetailListByOdrId(pOrderDetailAndPInventoryModel.getOdrId());
                //如果是出库
                if (pOrderDetailAndPInventoryModel.getOdrType() == 2) {
                    //判断当前单据明细关联的库存是否因库存划项目，项目化库存和串料导致生成多条数据
//                    Integer pInventoryCount = purPOrderDetailDao.getPInventoryCountByRowNo(pOrderDetailAndPInventoryModel.getOdaId());
//                    if (pInventoryCount > 1) {
//                        return JsonResult.fail("红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModel.getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModel.getOdaMatDesc() + "的对应库存被串料、库存划项目或项目划库存成多条库存！");
//                    }
                    for (POrderDetail pOrderDetail : pOrderDetailList) {
                        //根据单据类型对库存操作
                        //执行红冲单据明细
                        pOrderDetail.setOdaRedFlag(1);
                        pOrderDetail.setOdaRedDate(new Date());
                        pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                        Integer odaId = pOrderDetail.getOdaId();
                        //标记被红冲数据
                        purPOrderDetailDao.updateByPrimaryKeySelective(pOrderDetail);
                        Double redNum = 0 - pOrderDetail.getOdaNum();
                        pOrderDetail.setOdaNum(redNum);
                        pOrderDetail.setOdaRushId(pOrderDetail.getOdaId());
                        pOrderDetail.setOdaRedFlag(2);
                        pOrderDetail.setOdaRedDate(new Date());
                        pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                        pOrderDetail.setOdaBrand(pOrderDetail.getOdaBrand());
                        //标记红冲数据
                        purPOrderDetailDao.insert(pOrderDetail);
                        //如果是出库则增加库存
                        PInventory pInventory = purPInventoryDao.getPInventoryByPK(pOrderDetail.getOdaOutItyId());
                        pInventory.setItyNum(pInventory.getItyNum() + Math.abs(redNum));
                        pInventory.setItyAvaNum(pInventory.getItyAvaNum() + Math.abs(redNum));
                        purPInventoryDao.updateNumByPrimaryKey(pInventory);
                        //执行对库存操作
                        logService.saveLog("红冲", "被红冲单号：" + pOrderDetailAndPInventoryModel.getOdrCode() + ";被红冲物料名称：" + pOrderDetail.getOdaMatDesc() + ";单据类型：调拨单-出库");
                    }
                }
                //如果是入库单
                else if (pOrderDetailAndPInventoryModel.getOdrType() == 1) {
                    //检测所有入库的明细库存在库数量是否支持冲单
                    for (POrderDetail pOrderDetail : pOrderDetailList) {
                        //依据外购件库存行号（入库单主键）获取对应的库存
                        PInventory pInventory = purPInventoryDao.getPInventoryByRowNo(pOrderDetail.getOdaId());
                        if(pInventory.getItyNum()<pOrderDetail.getOdaNum()){
                            return JsonResult.fail("红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModel.getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModel.getOdaMatDesc() + "的对应库存不足无法支持冲单！");
                        }
                    }
                    for (POrderDetail pOrderDetail : pOrderDetailList) {
                        //根据单据类型对库存操作
                        //执行红冲单据明细
                        pOrderDetail.setOdaRedFlag(1);
                        pOrderDetail.setOdaRedDate(new Date());
                        pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                        Integer odaId = pOrderDetail.getOdaId();
                        //标记被红冲数据
                        purPOrderDetailDao.updateByPrimaryKeySelective(pOrderDetail);
                        Double redNum = 0 - pOrderDetail.getOdaNum();
                        pOrderDetail.setOdaNum(redNum);
                        pOrderDetail.setOdaRushId(pOrderDetail.getOdaId());
                        pOrderDetail.setOdaRedFlag(2);
                        pOrderDetail.setOdaRedDate(new Date());
                        pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                        pOrderDetail.setOdaBrand(pOrderDetail.getOdaBrand());
                        //标记红冲数据
                        purPOrderDetailDao.insert(pOrderDetail);
                        //如果是入库则减少库存
                        PInventory pInventory = purPInventoryDao.getPInventoryByRowNo(odaId);
                        pInventory.setItyNum(pInventory.getItyNum() - Math.abs(redNum));
                        pInventory.setItyAvaNum(pInventory.getItyAvaNum() - Math.abs(redNum));
                        purPInventoryDao.updateNumByPrimaryKey(pInventory);
                        //执行对库存操作
                        logService.saveLog("红冲", "被红冲单号：" + pOrderDetailAndPInventoryModel.getOdrCode() + ";被红冲物料名称：" + pOrderDetail.getOdaMatDesc() + ";单据类型：调拨单-入库");
                    }
                }

                if (allocationOrder.getOdrOutStatus() == 3) {
                    allocationOrder.setOdrOutStatus(2);
                }else if (allocationOrder.getOdrOutStatus() == 2) {
                    allocationOrder.setOdrOutStatus(1);
                }
                purPOrderDetailDao.updateAllocationOrderOdrOutStatusByOdrId(allocationOrder);

//                Integer reverseReqCount = pAllocationRecordDao.getReverseReqCountByArdOdrId(pOrderDetailAndPInventoryModel.getOdrId());
//                if (reverseReqCount > 0) {
//                    List<POrderDetail> pOrderDetailList = purPOrderDetailDao.findOrderDetailListByOdrId(pOrderDetailAndPInventoryModel.getOdrId());
//                    for (POrderDetail pOrderDetail : pOrderDetailList) {
//                        //执行红冲单据明细
//                        pOrderDetail.setOdaRedFlag(1);
//                        pOrderDetail.setOdaRedDate(new Date());
//                        pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
//                        Integer odaId = pOrderDetail.getOdaId();
//                        //标记被红冲数据
//                        purPOrderDetailDao.updateByPrimaryKeySelective(pOrderDetail);
//                        Double redNum = 0 - pOrderDetail.getOdaNum();
//                        pOrderDetail.setOdaNum(redNum);
//                        pOrderDetail.setOdaRushId(pOrderDetail.getOdaId());
//                        pOrderDetail.setOdaRedFlag(2);
//                        pOrderDetail.setOdaRedDate(new Date());
//                        pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
//                        //标记红冲数据
//                        purPOrderDetailDao.insert(pOrderDetail);
//                        //根据单据类型对库存操作
//                        if (pOrderDetail.getOdaType() == 2) {
//                            //如果是出库则增加库存
//                            PInventory pInventory = purPInventoryDao.getPInventoryByPK(pOrderDetail.getOdaOutItyId());
//                            pInventory.setItyNum(pInventory.getItyNum() + Math.abs(redNum));
//                            pInventory.setItyAvaNum(pInventory.getItyAvaNum() + Math.abs(redNum));
//                            purPInventoryDao.updateNumByPrimaryKey(pInventory);
//                        }
//                        //执行对库存操作
//                        logService.saveLog("红冲", "被红冲单号：" + pOrderDetailAndPInventoryModel.getOdrCode() + ";被红冲物料名称：" + pOrderDetail.getOdaMatDesc() + ";单据类型：调拨单");
//                    }
//                    //执行对调拨记录的作废
//                    pAllocationRecordDao.disableRecordByByOdrId(pOrderDetailAndPInventoryModel.getOdrId());
//
//                } else {
//                    JsonResult.fail("此调拨红冲失败，因此库存调拨已调拨到成品库，无法冲单。<br>如想冲单，请让成品库调拨入库操作人员对此调拨单进行冲单。<br>此单才可以再进行冲单！");
//                }
            }
            //判断是否是串料出库单，执行串料冲整单
            else if (pOrderDetailAndPInventoryModel.getOdrType() == 2 && purPInventoryDao.getPInventoryByPK(purPOrderDetailDao.findOrderDetailByOdaId(Integer.parseInt(param.getOdaIds())).getOdaOutItyId()).getItyChangeFlag() != null &&
                    purPInventoryDao.getPInventoryByPK(purPOrderDetailDao.findOrderDetailByOdaId(Integer.parseInt(param.getOdaIds())).getOdaOutItyId()).getItyChangeFlag() > 0 &&
                    pOrderDetailAndPInventoryModel.getOdaRedFlag() == 0
            ) {
                POrderDetail pOrderDetail = purPOrderDetailDao.findOrderDetailByOdaId(Integer.parseInt(param.getOdaIds()));
                //执行红冲单据明细
                pOrderDetail.setOdaRedFlag(1);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                //标记被红冲数据
                purPOrderDetailDao.updateByPrimaryKeySelective(pOrderDetail);
                Double redNum = 0 - pOrderDetail.getOdaNum();
                pOrderDetail.setOdaNum(redNum);
                pOrderDetail.setOdaRushId(pOrderDetail.getOdaId());
                pOrderDetail.setOdaRedFlag(2);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                pOrderDetail.setOdaBrand(pOrderDetail.getOdaBrand());
                //标记红冲数据
                purPOrderDetailDao.insert(pOrderDetail);
                //根据单据类型对库存操作
                //通过串料表获取库存数据
                if (pOrderDetail.getOdaType() == 2) {
                    //如果是出库则增加库存
                    PInventory pInventory = purPInventoryDao.getPInventoryByPK(pOrderDetail.getOdaOutItyId());
                    pInventory.setItyNum(pInventory.getItyNum() + Math.abs(redNum));
                    pInventory.setItyAvaNum(pInventory.getItyAvaNum() + Math.abs(redNum));
                    purPInventoryDao.updateNumByPrimaryKey(pInventory);
                    PChangeOrderModel pChangeOrderModel = null;
                    if (pInventory.getItyChangeFlag().equals(1) || pInventory.getItyChangeFlag().equals(3)) {
                        pChangeOrderModel = purChangeDao.getChangeCountByCeoNeedItyId(pInventory.getItyId());
                        if (pChangeOrderModel != null) {
                            pChangeOrderModel.setCeoAppyStatus(3);
                            purChangeDao.updatePChangeOrderCeoAppyStatusByPk(pChangeOrderModel);
                        }
                    }

                }
                logService.saveLog("红冲", "被红冲单号：" + pOrderDetailAndPInventoryModel.getOdrCode() + ";被红冲物料名称：" + pOrderDetail.getOdaMatDesc() + ";单据类型：串料单");
            }
            //如果是普通出入库单，判断是否因调整造成出入库单价、单位、物料描述发生变化，
            // 则冲回来的数据重新生成一条新的库存，新的行号与冲单明细主键关联。
            else if (pOrderDetailAndPInventoryModel.getOdrType() == 1 &&
                    pOrderDetailAndPInventoryModel.getOdrRedFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdrChangeFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdrReqFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdaNum() > purPInventoryDao.findInventoryByRowNumberRed(Integer.valueOf(param.getOdaIds())).getItyNum()) {
                return JsonResult.fail("红冲失败！<br>失败原因：单据编号为：" + pOrderDetailAndPInventoryModel.getOdrCode() + "。物料描述为：" + pOrderDetailAndPInventoryModel.getOdaMatDesc() + "的库存数量不足！");
            } else if (pOrderDetailAndPInventoryModel.getOdrType() == 1 &&
                    pOrderDetailAndPInventoryModel.getOdrRedFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdrChangeFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdrReqFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdaNum() <= purPInventoryDao.findInventoryByRowNumber(Integer.valueOf(param.getOdaIds())).getItyNum()
            ) {
                //冲入库单
                POrderDetail pOrderDetail = purPOrderDetailDao.findOrderDetailByOdaId(pOrderDetailAndPInventoryModel.getOdaId());
                //执行红冲单据明细
                pOrderDetail.setOdaRedFlag(1);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                Integer odaId = pOrderDetail.getOdaId();
                //标记被红冲数据
                purPOrderDetailDao.updateByPrimaryKeySelective(pOrderDetail);
                Double redNum = 0 - pOrderDetail.getOdaNum();
                pOrderDetail.setOdaNum(redNum);
                pOrderDetail.setOdaRushId(pOrderDetail.getOdaId());
                pOrderDetail.setOdaRedFlag(2);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                pOrderDetail.setOdaBrand(pOrderDetail.getOdaBrand());
                //标记红冲数据
                purPOrderDetailDao.insert(pOrderDetail);
                //根据单据类型对库存操作
                PInventory pInventory = purPInventoryDao.findInventoryByRowNumber(odaId);
                if (pOrderDetail.getOdaType() == 1) {
                    //如果是出库则增加库存
                    pInventory.setItyNum(pInventory.getItyNum() - Math.abs(redNum));
                    pInventory.setItyAvaNum(pInventory.getItyNum());
                }
                //执行对库存操作
                purPInventoryDao.updateNumByPrimaryKey(pInventory);
                logService.saveLog("红冲", "被红冲单号：" + pOrderDetailAndPInventoryModel.getOdrCode() + ";被红冲物料名称：" + pOrderDetail.getOdaMatDesc() + ";单据类型：入库单");
            }
            //如果是出库单直接入库即可
            else if (pOrderDetailAndPInventoryModel.getOdrType() == 2 &&
                    pOrderDetailAndPInventoryModel.getOdrRedFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdrChangeFlag() == 0 &&
                    pOrderDetailAndPInventoryModel.getOdrReqFlag() == 0
            ) {
                //冲出库单
                POrderDetail pOrderDetail = purPOrderDetailDao.findOrderDetailByOdaId(pOrderDetailAndPInventoryModel.getOdaId());
                //执行红冲单据明细
                pOrderDetail.setOdaRedFlag(1);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                Integer odaId = pOrderDetail.getOdaId();
                //标记被红冲数据
                purPOrderDetailDao.updateByPrimaryKeySelective(pOrderDetail);
                Double redNum = 0 - pOrderDetail.getOdaNum();
                pOrderDetail.setOdaNum(redNum);
                pOrderDetail.setOdaRushId(pOrderDetail.getOdaId());
                pOrderDetail.setOdaRedFlag(2);
                pOrderDetail.setOdaRedDate(new Date());
                pOrderDetail.setOdaRedUserId(ShiroUtils.getUser().getId());
                pOrderDetail.setOdaBrand(pOrderDetail.getOdaBrand());
                //标记红冲数据
                purPOrderDetailDao.insert(pOrderDetail);
                //根据单据类型对库存操作
                PInventory pInventory = purPInventoryDao.findInventoryByodaOutItyId(odaId);
                if (pOrderDetail.getOdaType() == 2) {
                    //如果是出库则增加库存
                    pInventory.setItyNum(pInventory.getItyNum() + Math.abs(redNum));
                    pInventory.setItyAvaNum(pInventory.getItyNum());
                }
                //执行对库存操作
                purPInventoryDao.updateNumByPrimaryKey(pInventory);
                logService.saveLog("红冲", "被红冲单号：" + pOrderDetailAndPInventoryModel.getOdrCode() + ";被红冲物料名称：" + pOrderDetail.getOdaMatDesc() + ";单据类型：出库单");
            }
        }
        return JsonResult.success();
    }


}